LINQ - Using System.Reflection with LINQ Queries


LINQ to SQL
LINQ to SQL, System.Reflection, Console Window
Desktop, Data
en-US
6/28/2011

Introduction

Use LINQ to right queries against the objects in your code using the Reflection APIs.

Building the Sample

Press F5

Description

The sample shows how to use methods from the System.Reflection namespace to run queries over types from System.Xml.Linq.dll. The by-product is an HTML document outlining the public APIs for the assembly.

The Reflector application generates an HTML document that outlines the public API for a given assembly. The task is achieved in two phases. First, an XML document is emitted in memory. (See method Emit*().) The document captures the relevant aspects of the assembly's metadata. In the second phase, an HTML document is extracted from the XML document. (See method Extract*().) Note that the two phases use the same query constructions to handle similar information in different representations.

Sample Code

 

C#
Edit|Remove
        XElement EmitAssembly(Assembly assembly)  
        { 
            return new XElement("assembly", 
                        new XAttribute("name", assembly.ManifestModule.Name), 
                        from type in assembly.GetTypes() 
                        where GetVisible(type) 
                        group type by GetNamespace(type) into g 
                        orderby g.Key 
                        select EmitNamespace(g.Key, g)); 
        }
 

 

Visual Basic
Edit|Remove
    Public Function EmitAssembly(ByVal assembly As Assembly) As XElement 
        Return <assembly name=<%= assembly.ManifestModule.Name %>> 
                   <%= From type In assembly.GetTypes() _ 
                       Where GetVisible(type) _ 
                       Group type By GetNamespace = GetNamespace(type) Into g = Group _ 
                       Order By GetNamespace _ 
                       Select EmitNamespace(GetNamespace, g) %> 
               </assembly> 
    End Function

Source Code Files